package net.chasing.androidbaseconfig.widget.rich;

import net.chasing.androidbaseconfig.widget.rich.base.AbstractRichParser;
import net.chasing.androidbaseconfig.widget.rich.base.RichItem;

import java.util.Locale;

public class TopicRichParser extends AbstractRichParser {
    private static final String LABEL_CONTENT = "<a data-huixueba-linkId=\"%d\">#%s#</a>";

    private TopicRichParser() {
        mLabelPattern = mPattern = "<a data-huixueba-linkId=\"[1-9][0-9]{0,}\">#.{1,}?#</a>";
        mRichFormat = " #%s# ";
        mIdPattern = "data-huixueba-linkId=\"[1-9][0-9]{0,}\"";
        mCharCountAfterName = 1;
    }

    private static final class MInstanceHolder {
        static final TopicRichParser mInstance = new TopicRichParser();//单例，全局保持一个解析实例
    }

    public static TopicRichParser getInstance() {
        return MInstanceHolder.mInstance;
    }

    /**
     * 将名字及ID转换成对应的<a></a>标签文本
     */
    public String getLabelText(String str, int topicId) {
        return String.format(Locale.CHINESE, LABEL_CONTENT, topicId, str);
    }

    public String changeTopicText(String richStr, String topicStr) {
        mTargetStr = richStr;
        RichItem firstRichItem = getFirstRichItem();
        if (firstRichItem != null)
            richStr = richStr.substring(firstRichItem.getEndPosition());
        return topicStr + richStr;
    }
}
